let global={};
$(function () {
    $.ajax({
        url:"./../server/cartsList.php",
        dataType:"json",
    }).then(function (res) {
        console.log(res);
        let{
            msg,
            status,
            data
        }=res;
        global=data;
        loadData(data);
        TotalPrice()
    })
    function loadData(data) {
        data.forEach((el,index)=>{
            var strHtml=``;
            strHtml+=`<tr data-tmpindex=${index}  class="bb1">`;
            strHtml+=`<td><input name="uname" type="checkbox" ></td>`;
            strHtml+=`<td class="th1"><img src=./../server/${el.p_url}></td>`;
            strHtml+=`<td class="th9">${el.p_name}</td>`;
            strHtml+=`<td class="th2">${el.p_type}</td>`;
            strHtml+=`<td class="th3">${el.p_price}</td>`;
            strHtml+=`<td class="th4"></td>`;
            strHtml+=`<td class="th5"></td>`;
            strHtml+=`<td class="th6"><button class='jian'>-</button><input value=${el.p_number}><button class='jia'>+</button></td>`;
            strHtml+=`<td class="th7">${el.p_total}</td>`;
            strHtml+=`<td class="th8"><a class="del" href='javascript:;;'>删除</a></td>`;
            strHtml+=`</tr>`;
            $("tbody").append(strHtml);
        })
    }
    $("table").on("click", ".jian", function () {
        var num = $(this).parents("td").find("input").val();
        if (num == 1) {
            return;
        }
        $(this).parents("td").find("input").val(--num);

        var price = $(this).parents("td").prev().prev().prev().text();
        $(this).parents("td").next().text(num * price);
        TotalPrice()

        var singleObj = global[$(this).parents("tr").data("tmpindex")];
        var updateObj = {
            cid: singleObj.c_id,
            num
        }

        $.ajax({
            url: "./../server/updateCarts1.php",
            data: updateObj,
            type: "post",
            dataType: "json"
        }).then(function (res) {
            console.log(res)
        })

    })

    $("table").on("click", ".jia", function () {

        var num = $(this).parents("td").find("input").val();

        $(this).parents("td").find("input").val(++num);

        var price = $(this).parents("tr").find(".th3").text();
        $(this).parents("td").next().text(num * price);
        TotalPrice()


        var singleObj = global[$(this).parents("tr").data("tmpindex")];
        var updateObj = {
            cid: singleObj.c_id,
            num
        }

        $.ajax({
            url: "./../server/updateCarts1.php",
            data: updateObj,
            type: "post",
            dataType: "json"
        }).then(function (res) {
            console.log(res)
        })

    })
    //删除
    $("table").on("click", ".del", function () {
        var singleObj = global[$(this).parents("tr").data("tmpindex")];
        var self=this;
        layer.confirm("删除", {
            icon: 3,
            btn: ["确定", "取消"]
        }, function (index) {
            $.ajax({
                url: "./../server/removeCarts.php",
                data: {
                    cid: singleObj.c_id
                },
                dataType: "json",
                type: "post"
            }).then(function (res) {
                if (res.status == 1) {
                    layer.msg("删除成功");
                    $(self).parents("tr").remove();
                }else{
                    layer.msg("删除失败")
                }
                layer.close(index); //关闭对话框
            })
        }, function () {
        })

    })
    function TotalPrice(){
        var opriceAll=0;
        var numall=0;
        var aaa=0
        $(".bb1").each(function(){
            aaa++;
            var price=parseInt($(this).find(".th3").text());

            var num=parseInt($(this).find(".th6 input").val());
            var priceOne=price*num;
            console.log(num)
            numall+=num;
            opriceAll+=priceOne;
            $(".center_c2_li1 span").text(aaa)
            $(".center_c2_li2 span").text(numall)
            $(".center_bm span").text(opriceAll)

        })
    }


})